// $codepro.audit.disable bracePosition, numericLiterals, variableShouldBeFinal
/*
 * Copyright (c) 2011 Team A CS2340. All rights reserved.
 */
package edu.gatech.teama.doctorsoffice.client.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;

import edu.gatech.teama.doctorsoffice.client.presenter.ServerConnection;
import edu.gatech.teama.doctorsoffice.model.Appointment;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Treatment Dialog
 * 
 * @author TEAMA
 * @version 1.0
 */
public class TreatmentDialog extends JDialog {

	/** a new jpanel*/
	private final JPanel contentPanel = new JPanel();
	
	/** a treatment record panel*/
	private TreatmentRecordPanel treatmentPanel;
	
	/** an instructions panel*/
	private PatientInstructionsPanel instructionsPanel;
	
	/** an appointment for this treatment record*/
	private Appointment appointment;

	/**
	 * Create the dialog.
	 *
	 * @param appointment the appointment
	 */
	public TreatmentDialog(Appointment appointment) { // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.constructorsOnlyInvokeFinalMethods
		treatmentPanel = new TreatmentRecordPanel(appointment.getTreatmentRecord());
		instructionsPanel = 
			new PatientInstructionsPanel(appointment.getTreatmentRecord());
		this.appointment = appointment;
		
		setBounds(100, 100, 450, 462);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setLayout(new FlowLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		this.setMinimumSize(new Dimension(800, 600));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						okPressed();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						cancelPressed();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		{
			JTabbedPane panel = new JTabbedPane();
			getContentPane().add(panel, BorderLayout.CENTER);
			{
				
				panel.addTab("Treatment Record", null, treatmentPanel, null);
			}
			{
				
				panel.addTab("Doctor's Orders", null, instructionsPanel, null);
			}
		}
	}
	
	/**
	 * Signifies that it has been pressed and tells the server and saves things
	 */
	private void okPressed() {
		ServerConnection sc = ServerConnection.getInstance();
		instructionsPanel.save();
		treatmentPanel.save();
		sc.updateAppointment(appointment);
		sc.updateUser(sc.getLoggedInUser());
		this.setVisible(false);
		this.dispose();
	}
	
	/**
	 * cancel pressed
	 */
	private void cancelPressed() {
		this.setVisible(false);
		this.dispose();
	}

}
